package com.pipayshop.controller;

import com.pipayshop.domain.vo.*;
import com.pipayshop.service.PhysicalStoreOrderInfoService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 实体店订单数据表 前端控制器
 * </p>
 *
 * @author wzx
 * @since 2023-08-01
 */
@Api(value = "实体店用户订单接口（最终版）",tags = "实体店用户订单接口（最终版）")
@RestController
@RequestMapping("/pipayshopapi/store-order")
@Slf4j
public class PhysicalStoreOrderController {

    @Resource
    private PhysicalStoreOrderInfoService shopOrderInfoService;

    /**
     * 查看未支付/已支付/已完成订单列表接口(通过一个标识id来获取对应的状态的列表展示)
     *  标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单
     */
    @PostMapping("getOrderList")
    @ApiOperation("（买家）用户的全部订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<OrderListVO>> getOrderList(@RequestBody GetOrderDataVO getOrderDataVO) {
        try {
            List<OrderListVO> orderList = shopOrderInfoService.getOrderList(getOrderDataVO);
            return ResponseVO.getSuccessResponseVo(orderList);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("user_order_list_retrieval_failed");
        }
    }


    @PostMapping("getOrderListByShopName")
    @ApiOperation("（买家）用户的全部订单列表展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<OrderListVO>> getOrderListByShopName(@RequestBody GetOrderByNameVO getOrderByNameVO) {
        try {
            List<OrderListVO> orderList = shopOrderInfoService.getOrderListByShopName(getOrderByNameVO);
            return ResponseVO.getSuccessResponseVo(orderList);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("user_order_list_retrieval_failed");
        }
    }

    /**
     删除订单接口
     */
    @PostMapping("delOrder/{orderId}")
    @ApiOperation("删除订单接口")
    public ResponseVO<String> delOrderByOrderId(@PathVariable("orderId") String orderId
                                               ) {
        try {
            int delete = shopOrderInfoService.delOrderByOrderId(orderId);
            if (delete < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("delete_order_success");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("删除订单失败");
        }
    }

    /**
     订单的详情接口
     */
    @GetMapping("getOrderDetail/{orderId}")
    @ApiOperation("订单（未支付 / 已支付）的详情接口")
    public ResponseVO<StoreOrderDetailVO> getOrderDetail(@PathVariable String orderId) {
        try {
            StoreOrderDetailVO orderDetail = shopOrderInfoService.getOrderDetail(orderId);
            if (orderDetail == null){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(orderDetail);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("order_detail_retrieval_failed");
        }
    }

    /**
     订单改为已完成状态接口
     */
    @PostMapping("completedOrder/{orderId}")
    @ApiOperation("订单已完成接口")
    public ResponseVO<String> completedOrder(@PathVariable String orderId) {
        try {
            int update = shopOrderInfoService.completedOrder(orderId);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("订单已完成成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("order_completion_failed");
        }
    }

    /**
     订单超时未支付的失效操作
     */
    @PostMapping("failOrder/{orderId}")
    @ApiOperation("订单超时未支付的失效操作接口")
    public ResponseVO<String> failOrder(@PathVariable String orderId) {
        try {
            int update = shopOrderInfoService.failOrder(orderId);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("订单超时未支付导致失效成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("order_timeout_unpaid_failed");
        }
    }
    @PostMapping("changePrice")
    @ApiOperation("未支付订单改价接口")
    public ResponseVO<String> changePrice(String token) {
        try {
            int update = shopOrderInfoService.changePrice(token);
            if (update < 1) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("update_unpaid_order_price_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("price_change_failed");
        }
    }
    /**
     定时轮询删除失效订单接口
     */
    @PostMapping("deleteFailOrders")
    @ApiOperation("定时轮询删除失效订单接口")
    public ResponseVO<String> deleteFailOrders() {
        try {
            shopOrderInfoService.deleteFailOrders();
            return ResponseVO.getSuccessResponseVo("scheduled_polling_delete_invalid_orders_success");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("delete_invalid_order_failed");
        }
    }

    /**
     * 生成未支付订单
     */
    @PostMapping("generateUnpaidOrder")
    @ApiOperation("生成未支付订单")
    public ResponseVO<String> generateUnpaidOrder(String token) {
        try {
            String orderId = shopOrderInfoService.generateUnpaidOrder(token);
            return ResponseVO.getSuccessResponseVo(orderId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("generate_unpaid_order_failed");
        }
    }


    /**
     支付下单接口
     */
    @PostMapping("payOrder")
    @ApiOperation("支付下单接口")
    public ResponseVO<String> payOrder(String token) {
        try {
            shopOrderInfoService.payOrder(token);
            return ResponseVO.getSuccessResponseVo("payment_order_success!");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("支付下单失败");
        }
    }


    /**
     * 查看未支付/已支付/已完成订单列表接口(通过一个标识id来获取对应的状态的列表展示)
     *  标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单
     */
    @PostMapping("getOrderListByShopId")
    @ApiOperation("(卖家)实体店的全部订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<PageDataVO> getOrderListByShopId(@RequestBody GetOrderDataVO getOrderDataVO) {
        try {
            PageDataVO list = shopOrderInfoService.getOrderListByShopId(getOrderDataVO);
            if (list == null){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("store_order_list_display_data_retrieval_failed");
        }
    }


}
